package com.jchy.maze;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;

import com.jchy.graph.Edge;
import com.jchy.graph.Vertex;
import com.jchy.graph.core.Pixel;

public class MazeAppPanel extends JPanel implements MouseMotionListener, MouseListener
{

	private static final long serialVersionUID = 999;

	public int xMin,xMax,yMin,yMax;
	
	private int linex, liney, boxx, boxy;

	private Maze theMaze;
	
	private ArrayList<Pixel> pix;
	
	private Color fill;
	
	private Color draw;
	
	public MazeAppPanel()
	{

		xMin=0; xMax=200; yMin=0; yMax=200;
        
		linex = 4; liney = 4; boxx = 25; boxy = 25;
		
		fill = new Color(0.0F, 0.0F, 0.0F);
		
		draw = new Color(1.0F, 0.0F, 0.0F);
		
		newMaze();
		
	}
	
	public void newMaze() {

		theMaze = new Maze(20,15);
		theMaze.buildGraphPossible();
		theMaze.buildMaze();
		
		pix = new ArrayList<Pixel>();
		pix.add(new Pixel(22,10,draw));
		
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
        
		int maxX = theMaze.getMaxX();
		int maxY = theMaze.getMaxY();
		int startX = 1;
		int endX = maxX;
		g.setColor(fill);
		int curx, cury;
		curx = 10; cury = 10;
		
		/* horiz line */
		/* top left corner */
		g.fillRect(curx,cury,linex,liney);
		curx=curx+linex;
		
		for(int x = 1; x <= maxX; x++)
		{
			boolean connectedDown = false;
			if(x==startX)
				connectedDown = true;
			
			if(connectedDown)
			{
				/* open space for maze entrance */
				curx=curx+boxx;
			}	
			else
			{
				/* rest of top line */
				g.fillRect(curx,cury,boxx,liney);
				curx=curx+boxx;
			}
			/* upper right corner */
			g.fillRect(curx,cury,linex,liney);
			curx=curx+linex;
		}
		/* new line */
		curx=10;
		cury=cury+liney;

		/* Maze Body */
		for(int y = 1; y <= maxY; y++)
		{
			/* vert lines */
			/* left edge */
			g.fillRect(curx, cury, linex, boxy);
			curx=curx+linex;

			for(int x = 1; x < maxX; x++)
			{
				Vertex curr = theMaze.getVertex(x, y);
				boolean connectedRight = false;
				Vertex right = theMaze.getVertex(x+1, y);
				Edge currRight = new Edge(curr,right);
				connectedRight = theMaze.getMaze().isEdgeInList(currRight);

				if( connectedRight )
				{
					/* skip the empty space */
					/* skip without drawing a wall */
					curx=curx+boxx;
					curx=curx+linex;
				}
				else
				{
					/* skip the empty space */
					curx=curx+boxx;
					/* draw the wall */
					g.fillRect(curx,cury,linex,boxy);
					curx=curx+linex;
				}
			}
			/* draw the last empty space */
			/* then draw the right edge */
			curx=curx+boxx;
			g.fillRect(curx,cury,linex,boxy);
			curx=curx+linex;

			/* start at the next row */
			curx=10;
			cury=cury+boxy;
			
			/* horiz line */
			/* Draw the left edge */
			g.fillRect(curx,cury,linex,liney);
			curx=curx+linex;
			for(int x = 1; x <= maxX; x++)
			{
				Vertex curr = theMaze.getVertex(x, y);
				boolean connectedDown = false;
				Vertex down = theMaze.getVertex(x, y+1);
				Edge currDown = new Edge(curr,down);
				if(y == maxY)
				{
					if(x == endX)
						connectedDown = true;
					else
						connectedDown = false;
				}
				else 
					connectedDown = theMaze.getMaze().isEdgeInList(currDown);
				
				if(connectedDown)
				{
					/* do not draw a wall */
					curx=curx+boxx;
				}
				else
				{
					/* draw the wall */
					g.fillRect(curx,cury,boxx,liney);
					curx=curx+boxx;
				}
				/* draw the right edge */
				g.fillRect(curx,cury,linex,liney);
				curx=curx+linex;
			}
			/* start at the next row */
			curx=10;
			cury=cury+liney;			
		}
		
		for(Pixel aPix : pix) {
			g.setColor(aPix.getTheColor());
			g.drawRect((int)aPix.getX(), (int)aPix.getY(), 1, 1);
		}
		
	} // paintcomponent
	
	public void mouseClicked(MouseEvent me)
	{
		System.out.println("MouseClicked: X-" + me.getX() + " Y-" + me.getY());
	}
  
	public void mouseMoved(MouseEvent me)
	{
		
	}
	
	public void mouseDragged(MouseEvent me)
	{
		System.out.println("MouseDragged: X-" + me.getX() + " Y-" + me.getY());
		
		for(Pixel aPix : pix) {
			System.out.println("Pix: X-" + aPix.getX() + " Y-" + me.getY());
			if(Math.abs(aPix.getX() - me.getX()) == 1 && Math.abs(aPix.getY() - me.getY()) == 1) {
				Pixel newPix = new Pixel(me.getX(), me.getX(), draw);
				pix.add(newPix);
				break;
			}
		}
	}

	public void mouseExited(MouseEvent me)
	{
		
	}

	public void mouseReleased(MouseEvent me)
	{
		
	}
	
	public void mouseEntered(MouseEvent me)
	{
		
	}
	
	public void mousePressed(MouseEvent me)
	{
		System.out.println("MousePressed: X-" + me.getX() + " Y-" + me.getY());
	}
}
